﻿using log4net;
using StoreProject.BLL;
using StoreProject.BLL.StackBLL;
using StoreProject.DAL;
using StoreProject.Entity;
using StoreProject.Entity.SettingEntity;
using StoreProject.Entity.StackEntity;
using StoreProject.Enum;
using StoreProject.Forms.Base;
using StoreProject.Forms.SettingForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.StackForms.Operate
{
    public partial class StackInQueryForm : ConfigInsertForm
    {
        ILog _log = LogManager.GetLogger("StackInQueryForm");
        ProductBLL productBLL = new ProductBLL();
        StackInBLL stackInBLL = new StackInBLL();
        StackLogBLL stackLogBLL = new StackLogBLL();
        public StackInQueryForm()
        {
            InitializeComponent();
        }
        #region 顶部菜单重写继承方法

        public override void Add()
        {
            StackInSettingForm stackInSettingForm = new StackInSettingForm(FormState.ADD);
            stackInSettingForm.TopMost = true;
            stackInSettingForm.ShowDialog();
            stackInSettingForm.BringToFront();
            this.Query();
        }


        public override void Query()
        {
            try
            {
                List<StackInEntity> data = new List<StackInEntity>();
                if (!string.IsNullOrEmpty(this.cmbProductName.Text) && !string.IsNullOrEmpty(this.cmbType.Text))
                {
                    data = stackInBLL.GetListByWhere(string.Format("ProductName like '%{0}%'  and Type like '%{1}%'", this.cmbProductName.Text, this.cmbType.Text)).ToList();
                }
                else if (!string.IsNullOrEmpty(this.cmbProductName.Text) )
                {
                    data = stackInBLL.GetListByWhere(string.Format("ProductName like '%{0}%' ", this.cmbProductName.Text)).ToList();
                }
                else if (!string.IsNullOrEmpty(this.cmbProductName.Text) && !string.IsNullOrEmpty(this.cmbType.Text))
                {
                    data = stackInBLL.GetListByWhere(string.Format("ProductName like '%{0}%'and Type like '%{1}%'", this.cmbProductName.Text, this.cmbType.Text)).ToList();
                }
                else if (!string.IsNullOrEmpty(this.cmbType.Text))
                {
                    data = stackInBLL.GetListByWhere(string.Format("Type like '%{0}%'", this.cmbType.Text)).ToList();
                }
                else
                {
                    data = stackInBLL.GetListEx().ToList();
                }
                if (data != null && data.Count > 0)
                {
                    this.uiDataGridView1.DataSource = data.OrderByDescending(x=>x.UpdateTime).ToList();
                }
                else
                {
                    uiDataGridView1.DataSource = null;
                    //cmbCustomer.DataSource = data;
                }

            }
            catch (Exception ex)
            {
                _log.Error("Query", ex);
            }
        }
        #endregion
        private void StackInQueryForm_Load(object sender, EventArgs e)
        {
            var list = productBLL.GetList().OrderByDescending(x => x.UpdateTime).ToList().Select(x => x.ProductName).ToArray();
            this.cmbProductName.Items.AddRange(list);

            uiDataGridView1.AutoGenerateColumns = false;
            List<StackInEntity> data = new List<StackInEntity>();
            data = stackInBLL.GetList().ToList();
            this.Query();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            this.Query();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.Add();
        }
    }
}
